<template>
  <div class="article">
    <el-card>
      <!-- 头部 面包屑组件 具名插槽-->
      <template #header>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <!-- 下方form表单  默认插槽 -->
      <el-form label-width="60px">
        <el-form-item label="状态">
          <!-- 单选框组件 -->
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部 </el-radio>
            <el-radio :label="0">草稿 </el-radio>
            <el-radio :label="1">待审核 </el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除 </el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 频道 -->
        <el-form-item label="频道">
         <Select :form="form"></Select>
         <!-- <Select v-model="form.channel_id"></Select> -->
        </el-form-item>
        <!-- 日期 -->
        <el-form-item label="日期">
          <!-- @change = 'pickerChange'  -->
          <el-date-picker
            v-model="form.picker"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <!-- 筛选按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submit">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <template #header> 根据筛选条件共查询到 {{ total }} 条结果： </template>
      <el-table :data="list">
        <!-- 封面 -->
        <el-table-column label="封面">
          <template v-slot="scope">
            <img class="cover" :src="scope.row.cover.images[0]" alt="" />
          </template>
        </el-table-column>
        <!-- 标题 -->
        <el-table-column label="标题" prop="title"></el-table-column>
        <!-- 状态 -->
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            {{ scope.row.status === 2 ? '审核通过' : '审核不通过' }}
          </template>
        </el-table-column>
        <!-- 发布时间 -->
        <el-table-column label="发布时间" width="300">
          <template v-slot="{ row }">
            {{ row.pubdate }}
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="read_count"></el-table-column>
        <el-table-column label="评论数" prop="comment_count"></el-table-column>
        <el-table-column label="点赞数" prop="like_count"></el-table-column>
        <el-table-column label="操作" width="250">
          <template v-slot="scope">
            <div>
              <h3>{{ scope.index }}</h3>
              <el-button @click="edit(scope.row)">编辑</el-button>
              <el-button @click="del(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :total="total"
        :page-size="form.per_page"
        :page-sizes="[1, 10, 20, 30, 40]"
        :current-page="form.page"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChange"
        @current-change="pageChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { channels, mpArticles, mpArticlesDelete } from '@/api/index.js'

export default {
  data () {
    return {
      form: {
        picker: [], // [起始时间,结束时间]
        status: null, // 文章状态，0-草稿，1-待审核，2-审核通过，3-审核失败，不传为全部
        channel_id: '', // 	不传为全部
        begin_pubdate: '', // 起始时间
        end_pubdate: '', // 截止时间
        page: 1, // 页码 默认为1页
        per_page: 10 // 每页数量 不传为默认10
      },
      channelsList: [], // 频道列表
      list: [], //文章列表
      total: 0 //总条数
    }
  },
  // 监听时间标签得变化
  watch: {
    'form.picker': {
      handler (newVal, oldVal) {
        // console.log(newVal)
        if (newVal.length === 2) {
          this.form.begin_pubdate = newVal[0]
          this.form.end_pubdate = newVal[1]
        }
      },
      // 开启深度监听  若不开启,只能监听到堆的变化
      depp: true
    }
  },
  // 初始进入使渲染数据
  async created () {
    // const res = await channels()
    // // console.log(res)
    // this.channelsList = res.data.channels
    this.getData()
  },
  methods: {
    // pickerChange (value) {
    //   if (value.length === 2) {
    //     this.form.begin_pubdate = value[0]
    //     this.form.end_pubdate = value[1]
    //   }
    // },
    // 获取数据
    async getData () {
      const res = await mpArticles(this.form)
      // console.log(res)
      this.list = res.data.results
      this.total = res.data.total_count
    },
    // 筛选按钮
    async submit () {
      // 筛选时 先让页码回到第一页 再调用请求方法渲染数据
      this.form.page = 1
      this.getData()
    },
    // 页容量改变
    sizeChange (size) {
      this.form.per_page = size
      // 当N条/页改变时   先让页码回到第一页  再调用请求方法渲染数据
      this.form.page = 1
      this.getData()
    },
    // 页码改变
    pageChange (page) {
      this.form.page = page
      this.getData()
    },
    // 删除方法 传入形参
    del (row) {
      this.$confirm('确认要删除数据吗?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log('确定');
          await mpArticlesDelete(row.id)
          this.$message.success('删除成功')
          this.submit()
        })
        .catch(() => {
          console.log('取消')
        })
    },
    edit(row){
      this.$router.push('/publish?id='+row.id)
    }
  }
}
</script>

<style>
.cover {
  width: 100px;
}
</style>
